// pages/index/index.js
var app = getApp()

import { getIndexData } from '../../config/index.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchKeyword: '',
    current: 0,
    bannerList: [],
    modList: [],
    cateList: [],
    mealList: [],
    hotPro: [],
    recPro: [],
    info: {
      product: {
        id: '',
        price: '',
        name: '',
        main_img: '',
        time: '',
        unit: '',
        service_category_id: '',
        content: ''
      },
      address: {
        id: '',
        name: '',
        phone: '',
        address: '',
        province_id: '',
        city_id: '',
        district_id: ''
      },
      time: {
        start_at: '',
        end_at: '',
        hours: 1
      },
      cleaner: {
        id: '',
        wx_avatar_url: '',
        real_name: ''
      },
      coupon: {
        id: '',
        price: '',
      },
      cleanList: [],
      remark: '',
      totalPrice: 0,
      textarea: true
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.init()
  },

  onPullDownRefresh: function() {
    wx.showNavigationBarLoading()
    this.init()
    wx.hideNavigationBarLoading()
    wx.stopPullDownRefresh()
  },

  /**
   * init
   */
  init: function () {
    let that = this,
        hotPro = [],
        recPro = [],
        i = 0,
        data
    wx.showLoading({
      mask:true,
      title: app.globalData.loadText,
    })
    getIndexData().then(res => {
      data = res.data
      for (; i < data.fast.length; i++) {
        if (i <= 2) {
          hotPro.push(data.fast[i])
        }
        if (i > 2) {
            recPro.push(data.fast[i])
        }
      }
      this.setData({
        bannerList: data.banner,
        cateList: data.category,
        mealList: data.setmeal_category,
        modList: data.middle,
        hotPro: hotPro,
        recPro: recPro,
        cleanList: data.provider
      })
      wx.hideLoading()
    }).catch(err => {
      console.log(2)
    })
  },

  /**
   * 获取保洁
   */
  getClean: function() {
    let that = this
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/v1/provider/choice-all?access_token=' + accessToken,
        success: function (res) {
          if(res.data.status === 1) {
            that.setData({
              cleanList: res.data.data.provider
            })
          } else {
            wx.showToast({
              title: res.data.msg,
            })

          }
        },
      })
    })
  },

  /**
   * 快捷支付
   */
  fastPay: function(e) {
    let _this = this,
        product_id = e.currentTarget.id,
        arr1 = ["1", "8", "11", "12", "17", "20", "24"],
        arr2 = ["9", "18", "19", "27", "28", "29", "32"],
        arr4 = ["30"]

    _this.setData({
      'info.product.id': product_id
    })

    if (_this.isInArray(arr1, product_id)) {
      wx.navigateTo({
        url: '/pages/product/first/first?info=' + JSON.stringify(_this.data.info),
      })
    }else if (_this.isInArray(arr2, product_id)) {
      wx.navigateTo({
        url: '/pages/product/second/second?info=' + JSON.stringify(_this.data.info),
      })
    } else if (_this.isInArray(arr4, product_id)) {
      wx.navigateTo({
        url: '/pages/product/third/third?info=' + JSON.stringify(_this.data.info),
      })
    } else {
      wx.navigateTo({
        url: '/pages/product/product?info=' + JSON.stringify(_this.data.info),
      })
    }
  },

  none: function(e) {
    wx.navigateTo({
      url: '/pages/merchant/merchant',
    })
  },

  cleanDetail: function(e) {
    let that = this

    that.setData({
      'info.cleaner.id': e.currentTarget.id
    })

    wx.navigateTo({
      url: '/pages/cleaner/detail/detail?info=' + JSON.stringify(that.data.info),
    })
  },

  /**
   *
   */
  takeCoupon: function(e) {
    let that = this
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/v1/customer/receive-give-coupon?access_token=' + accessToken,
        data: {
          coupon_id: e.currentTarget.id
        },
        success: res => {
          if(res.data.status === 1) {
            wx.showModal({
              showCancel: false,
              title: '提示',
              content: '领取成功，快去下单吧',
              success: res => {
                if(res.confirm) {
                  that.setData({
                    newWrap: false
                  })
                  that.init()
                }
              }
            })
          }
        }
      })
    })
  },

  /**
   * 数组去重
   */
  isInArray: function (arr, value) {
    for (var i = 0; i < arr.length; i++) {
      if (value === arr[i]) {
        return true;
      }
    }
    return false;
  },

  /**
   *
   */
  onShareAppMessage: function() {
    return {
      title: '7只仓鼠客户端'
    }
  }
})
